package com.cy.shiro.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.cy.shiro.pojo.SysResources;
import com.cy.shiro.service.SysResourcesService;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.util.CollectionUtils;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.apache.shiro.mgt.SecurityManager;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Autowired
    private SysResourcesService sysResourcesService;
    //创建ShiroFilterFactoryBean
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultSecurityManager securityManager){
//        System.out.println("创建bean");
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //设置安全管理器
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        // 添加Shiro内置过滤器
        /**
         * Shiro内置过滤器，可以实现权限相关的拦截器
         *  常用的过滤器：
         *      anon: 无需认证（登录）可以访问
         *      authc: 必须认证才可以访问
         *      user: 如果使用rememberMe功能可以直接访问
         *      perms: 该资源必须得到资源权限才可以访问
         *      role: 该资源必须得到角色权限才可以访问
         */
        Map<String, String> filerMap = new LinkedHashMap<>();
        filerMap.put("/postLogin", "anon"); //登陆页面不需要验证
        filerMap.put("/hello", "anon"); //登陆页面不需要验证
        filerMap.put("/logout", "anon"); //退出
        filerMap.put("/login", "anon"); //退出
        filerMap.put("/index", "authc"); //后台首页

        //授权过滤器(所有操作保存起来)
//        filerMap.put("/add", "perms[user:add]");
//        filerMap.put("/update", "perms[user:update]");
//        filerMap.put("/*", "authc");
//        filerMap.put("/*", "anon");

        //获取菜单权限
        List<SysResources> sysResources = sysResourcesService.listMenu();
        if(!CollectionUtils.isEmpty(sysResources)){
            for(SysResources resources : sysResources){
                filerMap.put(resources.getUrl(), "perms["+resources.getPermission()+"]");
            }
        }
//        System.out.println("quanxian:"+ filerMap);
        //获取菜单操作
        filerMap.put("/*", "authc");
        //设置登陆页面，发送login请求
        shiroFilterFactoryBean.setLoginUrl("/login");
        //设置未授权的页面
        shiroFilterFactoryBean.setUnauthorizedUrl("/noAuth");
        //设置过滤器
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filerMap);

        return shiroFilterFactoryBean;
    }

    /**
     * 开启shiro aop注解支持.
     * 使用代理方式;所以需要开启代码支持;
     *
     * @param securityManager
     * @return
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

    @Bean(name = "securityManager")
    public DefaultWebSecurityManager getDefaultSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(userRealm);
        return securityManager;
    }

    @Bean(name = "userRealm")
    public UserRealm getRealm(){
        return new UserRealm();
    }

    @Bean
    public ShiroDialect getShiroDialect(){
        return new ShiroDialect();
    }



}
